﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Threading;

namespace System.Extensions
{
    /// <summary>
    /// 实体转实体
    /// </summary>
    public class Conversion
    {
        /// <summary>
        /// 对象集合相互转化
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<T2> ConvertToList<T1, T2>(List<T1> source)
        {
            List<T2> t2List = new List<T2>();
            if (source == null || source.Count == 0)
                return t2List;
            PropertyInfo[] pi = typeof(T2).GetProperties();
            PropertyInfo[] pi1 = typeof(T1).GetProperties();
            foreach (T1 t1Model in source)
            {
                T2 model = Activator.CreateInstance<T2>();
                foreach (var p in pi)
                {
                    foreach (var p1 in pi1)
                    {
                        if (p.Name == p1.Name)
                        {
                            p.SetValue(model, p1.GetValue(t1Model, null), null);
                            break;
                        }
                    }
                }
                t2List.Add(model);
            }
            return t2List;
        }

        /// <summary>
        /// 单个实体转化
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static T2 ConvertToModel<T1, T2>(T1 source)
        {
            PropertyInfo[] pi = typeof(T2).GetProperties();
            PropertyInfo[] pi1 = typeof(T1).GetProperties();
            T2 model = Activator.CreateInstance<T2>();
            foreach (var p in pi)
            {
                foreach (var p1 in pi1)
                {
                    if (p.Name == p1.Name)
                    {
                        try { p.SetValue(model, p1.GetValue(source, null), null); } catch (Exception) { }
                        break;
                    }
                }
            }
            return model;
        }

        /// <summary>
        /// dic转Object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static T DicToObject<T>(Dictionary<string, object> dic) where T : new()
        {
            var md = new T();
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo textInfo = cultureInfo.TextInfo;
            foreach (var d in dic)
            {
                var filed = textInfo.ToTitleCase(d.Key);
                try
                {
                    var value = d.Value;
                    md.GetType().GetProperty(filed).SetValue(md, value);
                }
                catch (Exception)
                {

                }
            }
            return md;
        }
    }
}
